proto

#!/usr/bin/env php
<?php
/**
 * This file is to be a sample cli setup, like somebody would write if they were implementing this library.
 *
 * So let's try to implement 3 cli functions
 *
 * proto & proto main  ## print some useless information
 * proto rand -chars abc -len 20 ## generate a random number
 * proto evens -from 1 -to 113 
 *        ## get all even numbers in the range
 *        ## defaults to -from 0 -to 50
 *
 */

require(__DIR__.'/../src/Msgs.php');
require(__DIR__.'/../src/Cli.php');


$cli = new \Tlf\Cli();
// load_json_file fails silently if the file does not exist
$cli->load_json_file(__DIR__.'/defaults.json'); 
$cli->load_inputs(json_decode(file_get_contents(__DIR__.'/user_configs.json'),true));
$cli->load_stdin();

$cli->load_command('main',
    function($cli, $args){
        echo "pointless output";
    }
);

$cli->load_command('rand', 
    function($cli, $args){

        $chars = $args['chars'];
        $len = $args['len'];
        $random = '';
        $i = 0;
        while(++$i<=$len){
            $random .= 
                $chars[random_int(0,strlen($chars)-1)];
        }

        echo $random;
    }, "generate a random number from --chars string of --length int"
);

$cli->load_command('evens',
    function($cli, $args){
        extract($args);
        $i = $from;
        do {
            if ($i%2==0)echo $i."\n";
        } while ($i++<$to);
    }, "list even numbers. --from int --to int"
);




$cli->execute();